from typing import List

import requests

from geektime_0.service.litemall.api.api_base import ApiBase
from geektime_0.service.litemall.model.goods import Goods
from geektime_0.service.litemall.config.base_data import url


class LitemallHttpApi(ApiBase):

    # 商品列表
    def list_goods(self, keyword, page, limit) -> List[Goods]:
        r = requests.get(
            f'{url}/wx/goods/list',
            params = {'keyword': keyword, 'page': page, 'limit': limit},
            headers = {'X-Litemall-Token': self.session.get_token()}
        )
        data = r.json()['data']['list']
        if r.status_code == 200:
            goods_list = []
            for item in data:
                goods = Goods()
                goods.id = item['id']
                goods.name = item['name']
                goods.retailPrice = item['retailPrice']
                goods.counterPrice = item['counterPrice']
                goods_list.append(goods)
            return goods_list
        else:
            return []
